package com.wz.jiangsu.main.problem;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: wz
 * @author: wangzhou
 * @create: 2024-04-28 15:00
 * @description:
 **/
public class TwoSum {

    public static void main(String[] args) {
        int[] ints = twoSum(new int[]{2, 7, 11, 15}, 9000);
        System.out.println(getIntStr(ints));
    }
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[] { i, map.get(target - nums[i]) };
            }
            map.put(nums[i], i);
        }
        return new int[] {};
    }
    // 数组字符串
    public  static String getIntStr(int[] nums){
        StringBuilder sb = new StringBuilder();
        if (nums.length == 0) {
            sb.append("");
        } else {
            for (int num : nums) {
                sb.append(String.valueOf(num) + ",");
            }
        }
        String sbString = sb.toString();

        return "".equals(sbString) ? sbString : sb.substring(0,sb.length() -1);

    }

}
